using System;
using System.Collections.Generic;
using System.Text;

namespace GarbageTruck.DateRange
{
    public class DateRange : ITemporalExpression
    {

        #region CONSTRUCTORS

        public DateRange()
        {
        }

        public DateRange(string expresion)
        {
            string[] parameters = expresion.Split(',');
            this.FirstDate = DateTime.Parse(parameters[0]).Date;
            this.SecondDate = DateTime.Parse(parameters[1]).Date;
        }

        public DateRange(DateTime firstDate, DateTime secondDate)
        {
            this.FirstDate = firstDate.Date;
            this.SecondDate = secondDate.Date;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private DateTime m_FirstDate;
        private DateTime m_SecondDate;

        #endregion FIELDS

        #region PROPERTIES

        public DateTime FirstDate
        {
            get { return m_FirstDate; }
            set { m_FirstDate = value; }
        }

        public DateTime SecondDate
        {
            get { return m_SecondDate; }
            set { m_SecondDate = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            return (dateTime.Date >= FirstDate && dateTime.Date <= SecondDate);
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            if (dateTime.Date < FirstDate) return FirstDate;
            else if (dateTime.Date >= FirstDate && dateTime.Date.AddDays(1) <= SecondDate) return dateTime.Date.AddDays(1);
            else return null;
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            if (Contains(dateTime) == false) return null;
            return this.SecondDate.Add(new TimeSpan(24,59,59));
        }

        public override string ToString()
        {
            return string.Format("DRNG({0},{1})",
                this.FirstDate.ToString("yyyy/MM/dd"), 
                this.SecondDate.ToString("yyyy/MM/dd"));
        }

        #endregion METHODS

    }
}
